package com.ruoyi.web.controller.sujian;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysProject;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.staff.domain.StaffBaseinfo;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.domain.TenantInfo;
import com.ruoyi.system.service.ISysFloorService;
import com.ruoyi.system.service.ITenantInfoService;
import com.ruoyi.system.domain.SysUserProject;
import com.ruoyi.system.service.ISysProjectService;
import com.ruoyi.system.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 租户管理Controller
 *
 * @author bzs
 * @date 2021-07-05
 */
@Controller
@RequestMapping("/sujian/tenantManagement")
public class TenantManController extends BaseController
{
    private String prefix = "propertys/sujian/tenantManagement";


    @Autowired
    private ITenantInfoService tenantInfoService;

    @Autowired
    private ISysFloorService floorInfoService;


//    @RequiresPermissions("property:tenant:view")
    @GetMapping()
    public String tenant(ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        List<TenantInfo> tenantInfos = tenantInfoService.selectAllTenantInfo();
        modelMap.put("tenantInfos",tenantInfos);
        return prefix + "/main";
    }


    /**
     * 查询租户管理列表
     */
//    @RequiresPermissions("property:tenant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TenantInfo tenantInfo)
    {
        startPage();
        List<TenantInfo> list = tenantInfoService.selectTenantInfoList(tenantInfo);
        return getDataTable(list);
    }

    /**
     * 导入租户列表
     * */
    @Log(title = "租户管理", businessType = BusinessType.IMPORT)
//    @RequiresPermissions("property:tenant:import")
    @PostMapping("/importExcel")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TenantInfo> util = new ExcelUtil<>(TenantInfo.class);
        List<TenantInfo> lists = util.importExcel(file.getInputStream());
        SysUser sysUser = ShiroUtils.getSysUser();
        //校验数据
        String res = tenantInfoService.validateDatas(lists);
        if (StringUtils.isNotBlank(res)){
            return AjaxResult.error(res);
        }
        String message =tenantInfoService.importRelations(lists,updateSupport,sysUser);
        return AjaxResult.success(message);
    }


    /**
     * 导入中的下载模板
     * */
//    @RequiresPermissions("property:tenant:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TenantInfo> util = new ExcelUtil<TenantInfo>(TenantInfo.class);
        return util.importTemplateExcel("租户信息");
    }


    /**
     * 导出租户管理列表
     */
//    @RequiresPermissions("property:tenant:export")
    @Log(title = "租户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export()
    {
//        String[] tanantIds = Convert.toStrArray(ids);
        List<TenantInfo> list = tenantInfoService.selectAllTenantInfo();
        ExcelUtil<TenantInfo> util = new ExcelUtil<TenantInfo>(TenantInfo.class);
        return util.exportExcel(list, "租户管理");
    }

    /**
     * 新增租户管理
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/add";
    }

    /**
     * 新增保存租户管理
     */
//    @RequiresPermissions("property:tenant:add")
    @Log(title = "租户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TenantInfo tenantInfo)
    {
        return toAjax(tenantInfoService.insertTenantInfo(tenantInfo));
    }

    /**
     * 修改租户管理
     */
    @GetMapping("/edit/{tenantId}")
    public String edit(@PathVariable("tenantId") String tenantId, ModelMap modelMap)
    {
        TenantInfo tenantInfo = tenantInfoService.selectTenantInfoById(tenantId);
        modelMap.put("tenantInfo", tenantInfo);
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/edit";
    }

    /**
     * 修改保存租户管理
     */
//    @RequiresPermissions("property:tenant:edit")
    @Log(title = "租户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TenantInfo tenantInfo)
    {
        return toAjax(tenantInfoService.updateTenantInfo(tenantInfo));
    }

    /**
     * 删除租户管理
     */
//    @RequiresPermissions("property:tenant:remove")
    @Log(title = "租户管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tenantInfoService.deleteTenantInfoByIds(ids));
    }

}
